import 'package:flutter/widgets.dart';
import 'package:flutterlib/widgets/pull/nested/pull_load_sliver_widget.dart';
import 'base_pull_load_manager.dart';
import 'list_state.dart';
import 'pull_load_widget.dart';

typedef  GetDataLogic = Function(int value);
typedef  RenderItem = Widget Function(int value, dynamic entity);

class SimplePullLoadWidget extends StatefulWidget {

  final BasePullLoadManger pullLoadManger;

  SimplePullLoadWidget({Key key, this.pullLoadManger}) : super(key: key);

  @override
  SimplePullLoadWidgetState createState() => SimplePullLoadWidgetState();
}
class SimplePullLoadWidgetState extends State<SimplePullLoadWidget>
    with
        AutomaticKeepAliveClientMixin<SimplePullLoadWidget>,
        PullLoadListState<SimplePullLoadWidget>,
        TickerProviderStateMixin {
  @override
  bool get wantKeepAlive {
    return widget.pullLoadManger.wantKeepAlive;
  }

  @override
  requestRefresh() async {
    return await widget.pullLoadManger.getDataLogic(page);
  }

  @override
  requestLoadMore() async {
    return await widget.pullLoadManger.getDataLogic(page);
  }

  @override
  bool get isRefreshFirst => true;

  @protected
  bool get enablePullDown => widget.pullLoadManger.enablePullDown;


  @protected
  bool get enablePullUp => widget.pullLoadManger.enablePullUp;


  @override
  void initState() {
    super.initState();
    widget.pullLoadManger.setDataList(getDataList);
    widget.pullLoadManger.setRefreshController(refreshController);
    widget.pullLoadManger.setNotifyCallBack(notifyDataSetChange);
  }

  void notifyDataSetChange(Function callback) {
    setState(() {
      callback();
    });
  }

  @override
  Widget build(BuildContext context) {
    print(context.widget.toStringShort());
    super.build(context);
    return widget.pullLoadManger.isSliver ?
    PullLoadSliverWidget(
            pullLoadWidgetControl,
                (BuildContext context, int index) {return widget.pullLoadManger.renderItem(index, getListItem(index));},
            handleRefresh,
            onLoadMore,
            scrollController: widget.pullLoadManger.scrollController,
            headerSliverBuilder: widget.pullLoadManger.renderHeader,
            footerSliverBuilder: widget.pullLoadManger.renderFooter,
            refreshController: refreshController,
            enablePullDown: enablePullDown,
            enablePullUp: enablePullUp,
            emptyBuilder: widget.pullLoadManger.renderEmptyView,
          ) :
    PullLoadWidget(
      pullLoadWidgetControl,
          (BuildContext context, int index) {return widget.pullLoadManger.renderItem(index, getListItem(index));},
      handleRefresh,
      onLoadMore,
      scrollController: widget.pullLoadManger.scrollController,
      headerSliverBuilder: widget.pullLoadManger.renderHeader,
      footerSliverBuilder: widget.pullLoadManger.renderFooter,
      refreshController: refreshController,
      enablePullDown: enablePullDown,
      enablePullUp: enablePullUp,
      emptyBuilder: widget.pullLoadManger.renderEmptyView,
    );
  }

}